/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package bl;

import Interfaces.IClickable;
import Interfaces.IHoverable;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.util.LinkedList;
import resources.SpaceColors;

/**
 *
 * @author Alexander
 */
public class BasicButton extends BasicGraphicElement implements IClickable, IHoverable
{
    private String text = "";
    
    public BasicButton(float posX, float posY, float width, float height, LinkedList<BasicGraphicElement> childElements) {
        super(posX, posY, width, height, childElements);
    }

    public BasicButton(float posX, float posY, float width, float height, LinkedList<BasicGraphicElement> childElements, String text) {
        super(posX, posY, width, height, childElements);
        this.text = text;
    }

    @Override
    public void draw(Graphics2D g2d) 
    {
        g2d.setColor(SpaceColors.getBackgroundColor);
        g2d.fillRect((int)this.getPosX(), (int)this.getPosY(), (int)this.getWidth(), (int)this.getHeight());
        if(this.isIsPressed() == true)
        {
            g2d.setColor(SpaceColors.getFontColorPressed);
            g2d.setStroke(new BasicStroke(2));
            g2d.drawRoundRect((int)this.getPosX()+2, (int)this.getPosY()+2, (int)this.getWidth()-4, (int)this.getHeight()-4, 10, 10);
            g2d.setColor(SpaceColors.getFontColorPressed);
            g2d.setFont(new Font("Courier New", Font.BOLD, (int)(this.getHeight()/1.05)));
            if(g2d.getFontMetrics().stringWidth(text) >= (int)(this.getWidth())){ g2d.setFont(new Font("Courier New", Font.BOLD, (int)(this.getHeight()/1.30)));}
            g2d.drawString(text, (this.getPosX() + this.getWidth()/2) - (g2d.getFontMetrics().stringWidth(text)/2), (this.getPosY() + (float)(g2d.getFont().getSize()/1.35)));
        }
        else if(this.isIsHovered() == true)
        {
            g2d.setColor(SpaceColors.getFontColorHovered);
            g2d.setStroke(new BasicStroke(2));
            g2d.drawRoundRect((int)this.getPosX()+2, (int)this.getPosY()+2, (int)this.getWidth()-4, (int)this.getHeight()-4, 10, 10);
            g2d.setFont(new Font("Courier New", Font.BOLD, (int)this.getHeight()));
            if(g2d.getFontMetrics().stringWidth(text) >= (int)(this.getWidth())){ g2d.setFont(new Font("Courier New", Font.BOLD, (int)(this.getHeight()/1.25)));}
            g2d.drawString(text, (this.getPosX() + this.getWidth()/2) - (g2d.getFontMetrics().stringWidth(text)/2), (this.getPosY() + (float)(g2d.getFont().getSize()/1.35)));
        }
        else
        {
            g2d.setColor(SpaceColors.getBasicBorderColor);
            g2d.setStroke(new BasicStroke(2));
            g2d.drawRoundRect((int)this.getPosX()+2, (int)this.getPosY()+2, (int)this.getWidth()-4, (int)this.getHeight()-4, 10, 10);
            g2d.setFont(new Font("Courier New", Font.BOLD, (int)this.getHeight()));
            if(g2d.getFontMetrics().stringWidth(text) >= (int)(this.getWidth())){ g2d.setFont(new Font("Courier New", Font.BOLD, (int)(this.getHeight()/1.25)));}
            g2d.drawString(text, (this.getPosX() + this.getWidth()/2) - (g2d.getFontMetrics().stringWidth(text)/2), (this.getPosY() + (float)(g2d.getFont().getSize()/1.35)));
        }
        if(this.getChildElements() != null && !this.getChildElements().isEmpty())
        {
            for(BasicGraphicElement bge : this.getChildElements())
            {
                bge.draw(g2d);
            }
        }
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }


    @Override
    public void onRelease() {
        this.setIsPressed(false);
        if(text.equals("EXIT"))
        {
            System.exit(0);
        }
    }

    @Override
    public void onClick() {
        this.setIsPressed(true);
    }

    @Override
    public void onLeave() {
        this.setIsHovered(false);
    }

    @Override
    public void onEnter() {
        this.setIsHovered(true);
    }
    
    
}
